package application;

import Base.ResourceTest;
import com.huawei.translate.api.ApplicationApi;
import com.huawei.translate.model.Application;
import com.huawei.translate.notification.repository.EmailHistoryRepository;
import com.huawei.translate.notification.service.EmailTemplateType;
import com.huawei.translate.notification.service.NotificationService;
import com.huawei.translate.vo.application.ApplicationRequest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.SimpleDateFormat;
import java.util.Date;

import static org.mockito.Matchers.isA;

@RunWith(SpringRunner.class)
public class When_applicant_send_application extends ResourceTest{

    @Autowired
    private EmailHistoryRepository emailHistoryRepository;

    @Autowired
    @Mock
    private NotificationService notificationService;

    @Autowired
    @InjectMocks
    private ApplicationApi applicationApi;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void should_response_ok_when_applicant_send_valid_request() {

        Mockito.doNothing().when(notificationService).send(isA(EmailTemplateType.class), isA(Application.class));
        ApplicationRequest request = CreateApplicationRequest();

        ResponseEntity<Object> response = this.testRestTemplate.postForEntity("/api/applications",request,Object.class);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Assert.assertEquals(HttpStatus.CREATED,response.getStatusCode());
        Application application = applicationRepository.findAll().get(0);
        Assert.assertEquals(request.getAppName(),application.getAppName());
        Assert.assertEquals(sdf.format(request.getOnlineDate()),sdf.format(application.getOnlineDate()));
        Assert.assertEquals(request.getAccessCountPerSec(),application.getAccessCountPerSec());
        Assert.assertEquals(request.getLanguages(),application.getLanguages());
        Assert.assertEquals(request.getDescription(),application.getDescription());
        Assert.assertEquals(request.getProductOwner(),application.getProductOwner());
        Assert.assertEquals(request.getPoEmail(),application.getPoEmail());
        Assert.assertEquals(request.getInterfacePerson(),application.getInterfacePerson());
        Assert.assertEquals(request.getiPersonEmail(),application.getiPersonEmail());
    }

    @Test
    public void should_send_email_to_admin_when_submit_application() {

        Mockito.doNothing().when(notificationService).send(isA(EmailTemplateType.class), isA(Application.class));
        ApplicationRequest request = CreateApplicationRequest();

        ResponseEntity<Object> response = this.testRestTemplate.postForEntity("/api/applications",request,Object.class);

        Assert.assertEquals(HttpStatus.CREATED,response.getStatusCode());

        Mockito.verify(notificationService).send(isA(EmailTemplateType.class), isA(Application.class));
    }

    private ApplicationRequest CreateApplicationRequest() {
        ApplicationRequest request = new ApplicationRequest();
        request.setAppName("welink");
        request.setOnlineDate(new Date());
        request.setAccessCountPerSec("100");
        request.setLanguages("en;zh");
        request.setDescription("HelloWorld!!");
        request.setProductOwner("yyy");
        request.setPoEmail("po@huawei.com");
        request.setInterfacePerson("interface");
        request.setiPersonEmail("inter@163.com");
        return request;
    }

}
